<template>
  <div class="app-container">
    <el-card class="box-card">
      <template #header>
        <div class="card-header">
          <span>个人信息</span>
        </div>
      </template>
        <div style="text-align: center">
          <div class="el-upload">
            <img :src="userInfo.avatar ? userInfo.avatar : userInfo.avatar" title="点击上传头像" class="avatar">
          </div>
        </div>
        <ul class="user-info">
          <li><div style="height: 100%"><svg-icon icon-class="login" /> 登录账号<div class="user-right">{{ userInfo.username }}</div></div></li>
          <li><svg-icon icon-class="user1" /> 所在城市 <div class="user-right">{{ userInfo.city }}</div></li>
          <li><svg-icon icon-class="email" /> 用户邮箱 <div class="user-right">{{ userInfo.email }}</div></li>
          <li>
            <svg-icon icon-class="anq" /> 安全设置
            <div class="user-right" @click="dialogVisible = true">
              <a>修改密码</a>
            </div>
          </li>
        </ul>
   </el-card>
   <el-dialog
      v-model="dialogVisible"
      title="修改密码"
      width="30%"
      :close-on-click-modal="false"
      :before-close="cancel"
    >
      <el-form ref="ruleFormRef" :rules="rules" :model="form" label-width="100px">
        <el-form-item label="旧密码:" prop="currentPass">
          <el-input v-model="form.currentPass" style="width: 370px;" />
        </el-form-item>
        <el-form-item label="新密码:" prop="password">
          <el-input v-model="form.password" style="width: 370px;" />
        </el-form-item>
        <el-form-item label="确认密码:" prop="checkPass">
          <el-input v-model="form.checkPass" style="width: 370px;" />
        </el-form-item>
      </el-form>
      <div class="dialog-footer">
        <el-button type="text" @click="cancel">取消</el-button>
        <el-button :loading="loading" type="primary" @click="doSubmit(ruleFormRef)">确认</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script setup lang="ts">
import { reactive, ref } from 'vue'
import { useUserStore } from '@/store/user';
import { storeToRefs } from 'pinia';
import { updataPass } from '@/api/user'
import { ElMessage, FormInstance } from 'element-plus'

const dialogVisible = ref(false)
const ruleFormRef = ref<FormInstance>()

const useUser = useUserStore();
const { token, userInfo } = storeToRefs(useUser);

const confirmPass = (rule: any, value:any, callback:any) => {
  if (value) {
    if (form.password !== value) {
      callback(new Error('两次输入的密码不一致'))
    } else {
      callback()
    }
  } else {
    callback(new Error('请再次输入密码'))
  }
}

const rules = reactive({
  currentPass: [
    { required: true, message: '请输入旧密码', trigger: 'blur' }
  ],
  password: [
    { required: true, message: '请输入新密码', trigger: 'blur' },
    { min: 6, max: 20, message: '长度在 6 到 20 个字符', trigger: 'blur' }
  ],
  checkPass: [
    { required: true, validator: confirmPass, trigger: 'blur' }
  ]
})

const form = reactive({
  password: '',
  checkPass: '',
  currentPass: '',
})

const cancel = () => {
  dialogVisible.value = false
  form.password = ''
  form.checkPass = ''
  form.currentPass = ''
}

const doSubmit = (formEl: FormInstance | undefined) => {
  if (!formEl) return
  formEl.validate((valid) => {
    if (valid) {
      updataPass(form).then(res => {
        ElMessage.success('密码修改成功，请重新登录')
        setTimeout(() => {
          useUser.remove().then(res=>{
            location.reload()
          })
        }, 1500)
      }).catch(err => {
        ElMessage.error(err)
      })
    } else {
      return false
    }
  })
  
}
</script>

<style lang="scss" scoped>
.card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.text {
    font-size: 14px;
}

.item {
    margin-bottom: 18px;
}

.box-card {
    width: 480px;
}

 .avatar {
    width: 120px;
    height: 120px;
    border-radius: 50%;
  }
  .user-info {
    padding-left: 0;
    list-style: none;
    li{
      border-bottom: 1px solid #F0F3F4;
      padding: 11px 0;
      font-size: 13px;
    }
    .user-right {
      float: right;
      a{
        color: #317EF3;
      }
    }
  }
  .dialog-footer {
    display: flex;
    justify-content: center;
  }
</style>